package br.com.clicarros.dao.Implementacao.veiculo;

import br.com.clicarros.dao.Implementacao.AbstractDAO;
import br.com.clicarros.dominio.veiculo.Veiculo;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import br.com.clicarros.util.UtilHibernate;

/**
 *
 * @author Luana
 */

public class DAOVeiculo extends AbstractDAO {
   
    @Override
    public Object consultarId(Object o) throws ClassNotFoundException, SQLException, Exception {
        
        Veiculo v = (Veiculo) o;
        Session session = UtilHibernate.openSession();

       v  = (Veiculo)  session.load(v.getClass(), v.getIdVeiculo());  
       
       session.close();

       return v;
    }

    @Override
     public List<Object> consultar(Object o) throws SQLException, ClassNotFoundException, Exception {
         
         Veiculo v = (Veiculo) o;
         
       Session session = UtilHibernate.openSession();
       
       StringBuilder sql = new StringBuilder("from Veiculo as v where ");
       sql.append(" v.modelo like '%");
       sql.append(v.getModelo());
       sql.append("%'");
       sql.append("and v.versao like '%");
       sql.append(v.getVersao());
       sql.append("%'");
       sql.append("and v.marca.marca like '%");
       sql.append(v.getMarca().getMarca());
       sql.append("%'");
       sql.append("and v.categoria.categoria like '%");
       sql.append(v.getCategoria().getCategoria());
       sql.append("%'");

       Query q = session.createQuery(sql.toString());
       
       List<Object> retorno = q.list();
       return retorno;

    }       
            
}

